<?php
	
	
	class Container1 {

		protected static $instance = null;

		// 容器中的对象实例
		protected $instances = [];

		protected function __construct()
		{

		}

		public static function getInstance()
		{
			if (!self::$instance) {
				self::$instance = new self();
			}

			return self::$instance;
		}

		public function get($className)
		{

			if (isset($this->instances[$className])) {
				return call_user_func_array($this->instances[$className], []);
			}

			return null;
		}

		// 绑定对象到容器
		public function set($className, $concrete)
		{

			$this->instances[$className] = $concrete;
			return $concrete instanceof \Closure;
		}

	}



	// Db类
	class Db {

		public function __construct()
		{

		}

		public function select()
		{
			echo "我是查询";
		}

		public function delete()
		{
			echo "我是删除";
		}

		public function update()
		{
			echo "我是更新";
		}

	}


	// db类绑定到容器
	$res = Container1::getInstance()->set("db", function () {
		return new Db();
	});

	var_dump("Set res:" . $res . PHP_EOL . "<BR/>");


	// 从容器中取出对象
	Container1::getInstance()->get("db")->select();
	





	
?>